-- CALC_DRAGON_WISH_OPTIONS

---------------------------
-- 计算神龙许愿的选项
-- @param user   玩家对象
return function(user)
    -- 1. 先过滤出可以考虑的选项
    local allOptions = DragonWishM.getAllOptions();
    local list = {};
    local includeDragon = true;

    local dragonOption = DragonWishM.getOptionByRule("dragon");
    local collectionOpiton = DragonWishM.getOptionByRule("want_collection");
    local includeCollection = false;

    local hasMadeWish  = DragonWishM.hasMadeWish()

    -- 次数未用完的都加进来
    for index = 1, #allOptions do
        local id = allOptions[index];
        repeat
            local times = DragonWishM.getOptionTimesById(id);

            -- 如果没有许愿过，则无效愿望不出现
            if not hasMadeWish and DragonWishM.query(id, "is_effective") == 0 then
                break;
            end

            -- 神龙选项，已出现过则不再出现
            if DragonWishM.query(id, "rule") == "dragon" then
                if DragonWishM.getOptionTimesById(id) > 0 or PetM.getMyPet(21) ~= nil then
                    includeDragon = false;
                end
                break;
            end

            -- 如果是神龙碎片选项，只有等神龙冈布奥选项出现过后才会出现
            if DragonWishM.query(id, "rule") == "dragon_scrap" then
                if DragonWishM.getOptionTimesById(dragonOption.id) <= 0 and
                   PetM.getMyPet(21) == nil then
                    break;
                end
            end

            -- 如果是最残暴的装备选项，拿到两件装备后不再出现
            if DragonWishM.query(id, "rule") == "brutal_equipment" then
                local brutalEquipment = DungeonM.getDungeonMixedField("brutal_equipment");
                if brutalEquipment ~= nil and brutalEquipment >= 3 then
                    break;
                end
            end

            -- 如果是我想获得巨龙之力选项，已经可以学习就不再出现
            if DragonWishM.query(id, "rule") == "want_power" then
                local dragonPower = DungeonM.getDungeonMixedField("dragon_power");
                if dragonPower ~= nil and dragonPower >= 1 then
                    break;
                end
            end

            -- 如果是我想获得龙语魔法选项，已经可以学习就不再出现
            if DragonWishM.query(id, "rule") == "want_dragon_magic" then
                local dragonMagic = DungeonM.getDungeonMixedField("dragon_magic");
                if dragonMagic ~= nil and dragonMagic >= 1 then
                    break;
                end
            end

            -- 判断不过
            if not DragonWishM.canFetchWish(id) then
                break;
            end

            -- 限制了每日许愿次数的
            local maxTimes = DragonWishM.query(id, "daily_times");
            if maxTimes ~= NUMBER_INFINITY and DragonWishM.getDailyOptionTimesById(id) >= maxTimes then
                break;
            end

            -- 如果已经超出许愿次数了
            -- local maxTimes = DragonWishM.query(id, "times");
            -- if maxTimes ~= NUMBER_INFINITY and times >= maxTimes then
            --     break;
            -- end

            if times <= 0 and DragonWishM.query(id, "rule") ~= "dragon_scrap" then
                -- 神龙和神龙碎片以外的选项，有未完成的则不能触发神龙冈布奥
                includeDragon = false;
            end

            table.insert(list, id);

        until true;
    end

    -- 如果是人生第一次则固定出现金币、钻石、伙伴
    if not hasMadeWish then
        list = { 1, 3, 6, };
    end

    -- 如果是在迷之森，固定出现金币、伙伴、矿石
    local dungeonId = DungeonM.getDungeonId();
    if dungeonId == 30003 then
        list = { 1, 3, 5, };
    elseif table.indexOf({40001, 40002, 40003, 40004, 40005}, dungeonId) > 0 then
        list = { 1, 3, 6, };
    end

    -- 排序一遍确保客户端/服务器的顺序一致
    table.sort(list);

    -- 2. 随机抽3发
    local ret = {};
    local len = #list;
    local count = math.min(3, len);
    for i = 1, count do
        local rand = DungeonM.getRandSeed("dragon_wish_option") % len;
        local id = list[rand + 1];

        -- 交给模块抽取数据
        ret[i] = DragonWishM.fetchOption(id);

        list[rand + 1] = list[len];
        len = len - 1;

        if DragonWishM.query(id, "rule") == "want_collection" then
            includeCollection = true;
        end

    end

    -- 神龙冈布奥可获取时，必定出现选项（替换掉随机的最后一个选项）
    if includeDragon and type(dragonOption.id) == "number" then
        ret[#ret] = DragonWishM.fetchOption(dragonOption.id);
    end



    -- 如果可以获得拼图的话需要固定出《我要你的收藏品》，（替换掉随机的第一个）
    local puzzleList = DungeonPuzzleM.calcCollectionPuzzle() or {};
    if #puzzleList > 0 and not includeCollection then
        ret[1] = DragonWishM.fetchOption(collectionOpiton.id);
    end

    return ret;
end
